package com.neusoft.neuiotms.dcom.converter;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.bizcore.web.utils.id.BaseIdGenerator;
import com.neusoft.neuiotms.dcom.dto.InstitutionsDTO;
import com.neusoft.neuiotms.dcom.model.Institutions;
import com.neusoft.neuiotms.dcom.model.InstitutionsUser;
import com.neusoft.neuiotms.dcom.model.ResidentUser;
import com.neusoft.neuiotms.dcom.repository.InstitutionsRepository;
import com.neusoft.neuiotms.dcom.repository.InstitutionsUserRepository;
import com.neusoft.neuiotms.dcom.repository.ResidentUserRepository;

@Component
public class InstitutionsConverter extends AbstractConverter<Institutions, InstitutionsDTO> {

    @Autowired
    private InstitutionsRepository institutionsRepository;
    @Autowired
    private InstitutionsUserConverter institutionsUserConverter;
    @Autowired
    private ResidentUserConverter residentUserConverter;
    @Autowired
    private InstitutionsUserRepository institutionsUserRepository;

    @Autowired
    private ResidentUserRepository residentUserRepository;
    @Autowired
    private BaseIdGenerator institutionsCodeGenerator;

    @Override
    public Institutions toModel(final InstitutionsDTO dto) {

        Institutions model;
        if (dto.isNew()) {
            model = new Institutions();
            model.setCode(this.institutionsCodeGenerator.generateCode());
        } else {
            model = this.institutionsRepository.getOne(dto.getId());
            model.setCode(dto.getCode());
        }

        model.setName(dto.getName());
        model.setLng(dto.getLng());
        model.setLat(dto.getLat());

        final List<ResidentUser> residents = model.getResidents().stream().collect(Collectors.toList());

        residents.forEach(k -> {
            model.getResidents().remove(k);
            this.residentUserRepository.delete(k);
        });

        dto.getResidents().stream().map(s -> {
            final ResidentUser user = this.residentUserConverter.toModel(s);
            user.setInstitutions(model);
            return user;
        }).forEach(model.getResidents()::add);

        final List<InstitutionsUser> users = model.getUsers().stream().collect(Collectors.toList());

        users.forEach(k -> {
            model.getUsers().remove(k);
            this.institutionsUserRepository.delete(k);
        });

        dto.getUsers().stream().map(s -> {
            final InstitutionsUser user = this.institutionsUserConverter.toModel(s);
            user.setInstitutions(model);
            return user;
        }).forEach(model.getUsers()::add);

        return model;
    }

    @Override
    public InstitutionsDTO toDTO(final Institutions model, final boolean forListView) {

        final InstitutionsDTO dto = new InstitutionsDTO();
        dto.setId(model.getId());
        dto.setCode(model.getCode());
        dto.setName(model.getName());
        dto.setLng(model.getLng());
        dto.setLat(model.getLat());
        //        dto.setResidentUserCode(model.getResidentUserCode());
        //        dto.setResidentUserName(model.getResidentUserName());

        if (null != model.getResidents()) {
            model.getResidents().stream().map(l -> this.residentUserConverter.toDTO(l))
                    .forEach(dto.getResidents()::add);
        }
        if (null != model.getUsers()) {
            model.getUsers().stream().map(l -> this.institutionsUserConverter.toDTO(l)).forEach(dto.getUsers()::add);
        }

        return dto;
    }

}
